package com.bw.home.viewmodel

import android.util.Log
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.repeatOnLifecycle
import androidx.lifecycle.viewModelScope
import com.bw.home.model.HomeListEntity
import com.bw.home.model.HomeRepo
import com.bw.lib_base.base.BaseViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class HomeViewModel @Inject constructor(val homeRepo: HomeRepo):BaseViewModel() {
    private val _homeData = MutableStateFlow<HomeListEntity?>(null)
    val homeData:StateFlow<HomeListEntity?> = _homeData

    fun getHome(){
        viewModelScope.launch {
            Log.d("===","getHome")
            if(isInitLife()){
                Log.d("===","生命周期？：执行")
                lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED){
                    val result = homeRepo.getHomeList()
                    _homeData.emit(result)
                }
            }
        }
    }
}